<?php
namespace app\index\relevan;
use app\index\controller\Common;
use app\index\model\BidModel;
use app\index\model\CertBorrowListModel;
use app\index\model\CertCompanyModel;
use app\index\model\CertModel;
use app\index\model\CertPersonModel;
use app\index\model\CertUseBidListModel;
use app\index\model\CertUseBidModel;
use app\index\model\CertUseListModel;
use app\index\model\CertUseModel;
use app\index\model\EnginModel;

class CertRelevan extends Common
{

    //更新证书使用情况 使用   审批通过的时候
    public function set_cert_statueuse_bid_yong($data, $bill){
        $exist = CertUseBidModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['isreal'=>1,'bidid'=>$data['bidid']])->find();
        if($exist){
            CertUseBidModel::where(['id'=>$exist['id']])->update(['isreal'=>0]);
            $existList = CertUseBidListModel::where(['useid'=>$exist['id'],'opt'=>1])->select()->toArray();
            foreach ($existList as $eee) {
                $certStatueuse = 1;

                $bidList = CertUseBidListModel::alias('list')
                    ->field('use.bidid,list.certid')
                    ->leftJoin('cert_use_bid use', 'use.id = list.useid')
                    ->where(['use.comid' => $this->userinfo['comid'], 'use.deleted_user' => 0])
                    ->where(['use.isturn' => 1, 'use.status' => 1, 'use.isreal' => 1, 'list.opt' => 1])
                    ->where(['list.certid'=>$eee['certid']])
                    ->select()->toArray();
                foreach ($bidList as $hhh){
                    $bidInfo = BidModel::where(['id'=>$hhh['bidid']])->find();
                    if($bidInfo['deleted_user'] == 0 && $bidInfo['status'] == 1 && $bidInfo['isqi'] == 0){
                        if($bidInfo['state'] == 1 || $bidInfo['state'] == 3){
                            if($certStatueuse <= 3){
                                $certStatueuse = 3;
                            }
                        }
                    }
                }

                $enginList = CertUseListModel::alias('list')
                    ->field('use.enginid,list.certid,list.issuo')
                    ->leftJoin('cert_use use', 'use.id = list.useid')
                    ->where(['use.comid' => $this->userinfo['comid'], 'use.deleted_user' => 0])
                    ->where(['use.isturn' => 1, 'use.status' => 1, 'use.isreal' => 1, 'list.opt' => 1])
                    ->where(['list.certid'=>$eee['certid']])
                    ->select()->toArray();
                foreach ($enginList as $hhh){
                    $enginInfo = EnginModel::where(['id'=>$hhh['enginid']])->find();
                    if($enginInfo['deleted_user'] == 0 && $enginInfo['status'] == 1){
                        if($enginInfo['state'] == 0 || $enginInfo['state'] == 3){
                            if($hhh['issuo'] == '是'){
                                $certStatueuse = 7;
                            }else{
                                if($certStatueuse <= 5){
                                    $certStatueuse = 5;
                                }
                            }
                        }
                    }
                }
                CertModel::where(['id'=>$eee['certid']])->update(['statueuse'=>$certStatueuse]);
                $personStatueuse = 1;
                if($certStatueuse > 1){
                    $personStatueuse = 2;
                }
                $personid = CertModel::where(['id'=>$eee['certid']])->value('personid');
                CertPersonModel::where(['id'=>$personid])->update(['statueuse'=>$personStatueuse]);
            }
        }





        CertUseBidModel::where(['id'=>$data['id']])->update(['isreal'=>1]);
        $list = CertUseBidListModel::where(['useid'=>$data['id'],'opt'=>1])->select()->toArray();
        foreach ($list as $vvv){
            $certStatueuse = 1;

            $bidList = CertUseBidListModel::alias('list')
                ->field('use.bidid,list.certid')
                ->leftJoin('cert_use_bid use', 'use.id = list.useid')
                ->where(['use.comid' => $this->userinfo['comid'], 'use.deleted_user' => 0])
                ->where(['use.isturn' => 1, 'use.status' => 1, 'use.isreal' => 1, 'list.opt' => 1])
                ->where(['list.certid'=>$vvv['certid']])
                ->select()->toArray();
            foreach ($bidList as $hhh){
                $bidInfo = BidModel::where(['id'=>$hhh['bidid']])->find();
                if($bidInfo['deleted_user'] == 0 && $bidInfo['status'] == 1 && $bidInfo['isqi'] == 0){
                    if($bidInfo['state'] == 1 || $bidInfo['state'] == 3){
                        if($certStatueuse <= 3){
                            $certStatueuse = 3;
                        }
                    }
                }
            }

            $enginList = CertUseListModel::alias('list')
                ->field('use.enginid,list.certid,list.issuo')
                ->leftJoin('cert_use use', 'use.id = list.useid')
                ->where(['use.comid' => $this->userinfo['comid'], 'use.deleted_user' => 0])
                ->where(['use.isturn' => 1, 'use.status' => 1, 'use.isreal' => 1, 'list.opt' => 1])
                ->where(['list.certid'=>$vvv['certid']])
                ->select()->toArray();
            foreach ($enginList as $hhh){
                $enginInfo = EnginModel::where(['id'=>$hhh['enginid']])->find();
                if($enginInfo['deleted_user'] == 0 && $enginInfo['status'] == 1){
                    if($enginInfo['state'] == 0 || $enginInfo['state'] == 3){
                        if($hhh['issuo'] == '是'){
                            $certStatueuse = 7;
                        }else{
                            if($certStatueuse <= 5){
                                $certStatueuse = 5;
                            }
                        }
                    }
                }
            }

            CertModel::where(['id'=>$vvv['certid']])->update(['statueuse'=>$certStatueuse]);
            $personid = CertModel::where(['id'=>$vvv['certid']])->value('personid');
            CertPersonModel::where(['id'=>$personid])->update(['statueuse'=>2]);
        }

    }
    public function set_cert_statueuse_engin_yong($data, $bill){
        $exist = CertUseModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['isreal'=>1,'enginid'=>$data['enginid']])->find();
        if($exist){
            CertUseModel::where(['id'=>$exist['id']])->update(['isreal'=>0]);
            $existList = CertUseListModel::where(['useid'=>$exist['id'],'opt'=>1])->select()->toArray();
            foreach ($existList as $eee) {
                $certStatueuse = 1;

                $bidList = CertUseBidListModel::alias('list')
                    ->field('use.bidid,list.certid')
                    ->leftJoin('cert_use_bid use', 'use.id = list.useid')
                    ->where(['use.comid' => $this->userinfo['comid'], 'use.deleted_user' => 0])
                    ->where(['use.isturn' => 1, 'use.status' => 1, 'use.isreal' => 1, 'list.opt' => 1])
                    ->where(['list.certid'=>$eee['certid']])
                    ->select()->toArray();
                foreach ($bidList as $hhh){
                    $bidInfo = BidModel::where(['id'=>$hhh['bidid']])->find();
                    if($bidInfo['deleted_user'] == 0 && $bidInfo['status'] == 1 && $bidInfo['isqi'] == 0){
                        if($bidInfo['state'] == 1 || $bidInfo['state'] == 3){
                            if($certStatueuse <= 3){
                                $certStatueuse = 3;
                            }
                        }
                    }
                }

                $enginList = CertUseListModel::alias('list')
                    ->field('use.enginid,list.certid,list.issuo')
                    ->leftJoin('cert_use use', 'use.id = list.useid')
                    ->where(['use.comid' => $this->userinfo['comid'], 'use.deleted_user' => 0])
                    ->where(['use.isturn' => 1, 'use.status' => 1, 'use.isreal' => 1, 'list.opt' => 1])
                    ->where(['list.certid'=>$eee['certid']])
                    ->select()->toArray();
                foreach ($enginList as $hhh){
                    $enginInfo = EnginModel::where(['id'=>$hhh['enginid']])->find();
                    if($enginInfo['deleted_user'] == 0 && $enginInfo['status'] == 1){
                        if($enginInfo['state'] == 0 || $enginInfo['state'] == 3){
                            if($hhh['issuo'] == '是'){
                                $certStatueuse = 7;
                            }else{
                                if($certStatueuse <= 5){
                                    $certStatueuse = 5;
                                }
                            }
                        }
                    }
                }
                CertModel::where(['id'=>$eee['certid']])->update(['statueuse'=>$certStatueuse]);
                $personStatueuse = 1;
                if($certStatueuse > 1){
                    $personStatueuse = 2;
                }
                $personid = CertModel::where(['id'=>$eee['certid']])->value('personid');
                CertPersonModel::where(['id'=>$personid])->update(['statueuse'=>$personStatueuse]);
            }
        }





        CertUseModel::where(['id'=>$data['id']])->update(['isreal'=>1]);
        $list = CertUseListModel::where(['useid'=>$data['id'],'opt'=>1])->select()->toArray();
        foreach ($list as $vvv){
            $certStatueuse = 1;

            $bidList = CertUseBidListModel::alias('list')
                ->field('use.bidid,list.certid')
                ->leftJoin('cert_use_bid use', 'use.id = list.useid')
                ->where(['use.comid' => $this->userinfo['comid'], 'use.deleted_user' => 0])
                ->where(['use.isturn' => 1, 'use.status' => 1, 'use.isreal' => 1, 'list.opt' => 1])
                ->where(['list.certid'=>$vvv['certid']])
                ->select()->toArray();
            foreach ($bidList as $hhh){
                $bidInfo = BidModel::where(['id'=>$hhh['bidid']])->find();
                if($bidInfo['deleted_user'] == 0 && $bidInfo['status'] == 1 && $bidInfo['isqi'] == 0){
                    if($bidInfo['state'] == 1 || $bidInfo['state'] == 3){
                        if($certStatueuse <= 3){
                            $certStatueuse = 3;
                        }
                    }
                }
            }

            $enginList = CertUseListModel::alias('list')
                ->field('use.enginid,list.certid,list.issuo')
                ->leftJoin('cert_use use', 'use.id = list.useid')
                ->where(['use.comid' => $this->userinfo['comid'], 'use.deleted_user' => 0])
                ->where(['use.isturn' => 1, 'use.status' => 1, 'use.isreal' => 1, 'list.opt' => 1])
                ->where(['list.certid'=>$vvv['certid']])
                ->select()->toArray();
            foreach ($enginList as $hhh){
                $enginInfo = EnginModel::where(['id'=>$hhh['enginid']])->find();
                if($enginInfo['deleted_user'] == 0 && $enginInfo['status'] == 1){
                    if($enginInfo['state'] == 0 || $enginInfo['state'] == 3){
                        if($hhh['issuo'] == '是'){
                            $certStatueuse = 7;
                        }else{
                            if($certStatueuse <= 5){
                                $certStatueuse = 5;
                            }
                        }
                    }
                }
            }

            CertModel::where(['id'=>$vvv['certid']])->update(['statueuse'=>$certStatueuse]);
            $personid = CertModel::where(['id'=>$vvv['certid']])->value('personid');
            CertPersonModel::where(['id'=>$personid])->update(['statueuse'=>2]);
        }

    }

    //更新证书使用情况 释放
    public function set_cert_statueuse_bid_shi($data, $bill){
        CertUseBidModel::where(['id'=>$data['id']])->update(['isreal'=>0]);
        $list = CertUseBidListModel::where(['useid'=>$data['id'],'opt'=>1])->select()->toArray();
        foreach ($list as $eee) {
            $certStatueuse = 1;

            $bidList = CertUseBidListModel::alias('list')
                ->field('use.bidid,list.certid')
                ->leftJoin('cert_use_bid use', 'use.id = list.useid')
                ->where(['use.comid' => $this->userinfo['comid'], 'use.deleted_user' => 0])
                ->where(['use.isturn' => 1, 'use.status' => 1, 'use.isreal' => 1, 'list.opt' => 1])
                ->where(['list.certid'=>$eee['certid']])
                ->select()->toArray();
            foreach ($bidList as $hhh){
                $bidInfo = BidModel::where(['id'=>$hhh['bidid']])->find();
                if($bidInfo['deleted_user'] == 0 && $bidInfo['status'] == 1 && $bidInfo['isqi'] == 0){
                    if($bidInfo['state'] == 1 || $bidInfo['state'] == 3){
                        if($certStatueuse <= 3){
                            $certStatueuse = 3;
                        }
                    }
                }
            }

            $enginList = CertUseListModel::alias('list')
                ->field('use.enginid,list.certid,list.issuo')
                ->leftJoin('cert_use use', 'use.id = list.useid')
                ->where(['use.comid' => $this->userinfo['comid'], 'use.deleted_user' => 0])
                ->where(['use.isturn' => 1, 'use.status' => 1, 'use.isreal' => 1, 'list.opt' => 1])
                ->where(['list.certid'=>$eee['certid']])
                ->select()->toArray();
            foreach ($enginList as $hhh){
                $enginInfo = EnginModel::where(['id'=>$hhh['enginid']])->find();
                if($enginInfo['deleted_user'] == 0 && $enginInfo['status'] == 1){
                    if($enginInfo['state'] == 0 || $enginInfo['state'] == 3){
                        if($hhh['issuo'] == '是'){
                            $certStatueuse = 7;
                        }else{
                            if($certStatueuse <= 5){
                                $certStatueuse = 5;
                            }
                        }
                    }
                }
            }


            CertModel::where(['id'=>$eee['certid']])->update(['statueuse'=>$certStatueuse]);
            $personStatueuse = 1;
            if($certStatueuse > 1){
                $personStatueuse = 2;
            }
            $personid = CertModel::where(['id'=>$eee['certid']])->value('personid');
            CertPersonModel::where(['id'=>$personid])->update(['statueuse'=>$personStatueuse]);
        }

    }
    public function set_cert_statueuse_engin_shi($data, $bill){
        CertUseModel::where(['id'=>$data['id']])->update(['isreal'=>0]);
        $list = CertUseListModel::where(['useid'=>$data['id'],'opt'=>1])->select()->toArray();
        foreach ($list as $eee) {
            $certStatueuse = 1;

            $bidList = CertUseBidListModel::alias('list')
                ->field('use.bidid,list.certid')
                ->leftJoin('cert_use_bid use', 'use.id = list.useid')
                ->where(['use.comid' => $this->userinfo['comid'], 'use.deleted_user' => 0])
                ->where(['use.isturn' => 1, 'use.status' => 1, 'use.isreal' => 1, 'list.opt' => 1])
                ->where(['list.certid'=>$eee['certid']])
                ->select()->toArray();
            foreach ($bidList as $hhh){
                $bidInfo = BidModel::where(['id'=>$hhh['bidid']])->find();
                if($bidInfo['deleted_user'] == 0 && $bidInfo['status'] == 1 && $bidInfo['isqi'] == 0){
                    if($bidInfo['state'] == 1 || $bidInfo['state'] == 3){
                        if($certStatueuse <= 3){
                            $certStatueuse = 3;
                        }
                    }
                }
            }

            $enginList = CertUseListModel::alias('list')
                ->field('use.enginid,list.certid,list.issuo')
                ->leftJoin('cert_use use', 'use.id = list.useid')
                ->where(['use.comid' => $this->userinfo['comid'], 'use.deleted_user' => 0])
                ->where(['use.isturn' => 1, 'use.status' => 1, 'use.isreal' => 1, 'list.opt' => 1])
                ->where(['list.certid'=>$eee['certid']])
                ->select()->toArray();
            foreach ($enginList as $hhh){
                $enginInfo = EnginModel::where(['id'=>$hhh['enginid']])->find();
                if($enginInfo['deleted_user'] == 0 && $enginInfo['status'] == 1){
                    if($enginInfo['state'] == 0 || $enginInfo['state'] == 3){
                        if($hhh['issuo'] == '是'){
                            $certStatueuse = 7;
                        }else{
                            if($certStatueuse <= 5){
                                $certStatueuse = 5;
                            }
                        }
                    }
                }
            }


            CertModel::where(['id'=>$eee['certid']])->update(['statueuse'=>$certStatueuse]);
            $personStatueuse = 1;
            if($certStatueuse > 1){
                $personStatueuse = 2;
            }
            $personid = CertModel::where(['id'=>$eee['certid']])->value('personid');
            CertPersonModel::where(['id'=>$personid])->update(['statueuse'=>$personStatueuse]);
        }

    }

    //更新证书使用情况 更改投标状态的时候处理
    public function set_cert_statueuse_bid($bidid){
        $list = CertUseBidListModel::alias('list')
            ->field('list.certid')
            ->leftJoin('cert_use_bid use', 'use.id = list.useid')
            ->where(['use.comid' => $this->userinfo['comid'], 'use.deleted_user' => 0])
            ->where(['use.isturn' => 1, 'use.status' => 1, 'use.isreal' => 1, 'list.opt' => 1])
            ->where(['use.bidid'=>$bidid])
            ->select()->toArray();

        foreach ($list as $eee) {
            $certStatueuse = 1;

            $bidList = CertUseBidListModel::alias('list')
                ->field('use.bidid,list.certid')
                ->leftJoin('cert_use_bid use', 'use.id = list.useid')
                ->where(['use.comid' => $this->userinfo['comid'], 'use.deleted_user' => 0])
                ->where(['use.isturn' => 1, 'use.status' => 1, 'use.isreal' => 1, 'list.opt' => 1])
                ->where(['list.certid'=>$eee['certid']])
                ->select()->toArray();
            foreach ($bidList as $hhh){
                $bidInfo = BidModel::where(['id'=>$hhh['bidid']])->find();
                if($bidInfo['deleted_user'] == 0 && $bidInfo['status'] == 1 && $bidInfo['isqi'] == 0){
                    if($bidInfo['state'] == 1 || $bidInfo['state'] == 3){
                        if($certStatueuse <= 3){
                            $certStatueuse = 3;
                        }
                    }
                }
            }

            $enginList = CertUseListModel::alias('list')
                ->field('use.enginid,list.certid,list.issuo')
                ->leftJoin('cert_use use', 'use.id = list.useid')
                ->where(['use.comid' => $this->userinfo['comid'], 'use.deleted_user' => 0])
                ->where(['use.isturn' => 1, 'use.status' => 1, 'use.isreal' => 1, 'list.opt' => 1])
                ->where(['list.certid'=>$eee['certid']])
                ->select()->toArray();
            foreach ($enginList as $hhh){
                $enginInfo = EnginModel::where(['id'=>$hhh['enginid']])->find();
                if($enginInfo['deleted_user'] == 0 && $enginInfo['status'] == 1){
                    if($enginInfo['state'] == 0 || $enginInfo['state'] == 3){
                        if($hhh['issuo'] == '是'){
                            $certStatueuse = 7;
                        }else{
                            if($certStatueuse <= 5){
                                $certStatueuse = 5;
                            }
                        }
                    }
                }
            }


            CertModel::where(['id'=>$eee['certid']])->update(['statueuse'=>$certStatueuse]);
            $personStatueuse = 1;
            if($certStatueuse > 1){
                $personStatueuse = 2;
            }
            $personid = CertModel::where(['id'=>$eee['certid']])->value('personid');
            CertPersonModel::where(['id'=>$personid])->update(['statueuse'=>$personStatueuse]);
        }

    }

    //更新证书使用情况 更改项目状态的时候处理
    public function set_cert_statueuse_engin($enginid){
        $list = CertUseListModel::alias('list')
            ->field('list.certid')
            ->leftJoin('cert_use use', 'use.id = list.useid')
            ->where(['use.comid' => $this->userinfo['comid'], 'use.deleted_user' => 0])
            ->where(['use.isturn' => 1, 'use.status' => 1, 'use.isreal' => 1, 'list.opt' => 1])
            ->where(['use.enginid'=>$enginid])
            ->select()->toArray();

        foreach ($list as $eee) {
            $certStatueuse = 1;

            $bidList = CertUseBidListModel::alias('list')
                ->field('use.bidid,list.certid')
                ->leftJoin('cert_use_bid use', 'use.id = list.useid')
                ->where(['use.comid' => $this->userinfo['comid'], 'use.deleted_user' => 0])
                ->where(['use.isturn' => 1, 'use.status' => 1, 'use.isreal' => 1, 'list.opt' => 1])
                ->where(['list.certid'=>$eee['certid']])
                ->select()->toArray();
            foreach ($bidList as $hhh){
                $bidInfo = BidModel::where(['id'=>$hhh['bidid']])->find();
                if($bidInfo['deleted_user'] == 0 && $bidInfo['status'] == 1 && $bidInfo['isqi'] == 0){
                    if($bidInfo['state'] == 1 || $bidInfo['state'] == 3){
                        if($certStatueuse <= 3){
                            $certStatueuse = 3;
                        }
                    }
                }
            }

            $enginList = CertUseListModel::alias('list')
                ->field('use.enginid,list.certid,list.issuo')
                ->leftJoin('cert_use use', 'use.id = list.useid')
                ->where(['use.comid' => $this->userinfo['comid'], 'use.deleted_user' => 0])
                ->where(['use.isturn' => 1, 'use.status' => 1, 'use.isreal' => 1, 'list.opt' => 1])
                ->where(['list.certid'=>$eee['certid']])
                ->select()->toArray();
            foreach ($enginList as $hhh){
                $enginInfo = EnginModel::where(['id'=>$hhh['enginid']])->find();
                if($enginInfo['deleted_user'] == 0 && $enginInfo['status'] == 1){
                    if($enginInfo['state'] == 0 || $enginInfo['state'] == 3){
                        if($hhh['issuo'] == '是'){
                            $certStatueuse = 7;
                        }else{
                            if($certStatueuse <= 5){
                                $certStatueuse = 5;
                            }
                        }
                    }
                }
            }


            CertModel::where(['id'=>$eee['certid']])->update(['statueuse'=>$certStatueuse]);
            $personStatueuse = 1;
            if($certStatueuse > 1){
                $personStatueuse = 2;
            }
            $personid = CertModel::where(['id'=>$eee['certid']])->value('personid');
            CertPersonModel::where(['id'=>$personid])->update(['statueuse'=>$personStatueuse]);
        }

    }




    //借证 审批通过后 更新证书状态
    public function set_cert_goes_yong($data, $bill){
        $list = CertBorrowListModel::where(['mid'=>$data['id']])->select()->toArray();
        foreach ($list as $item){
            if($item['type'] == 1){
                CertModel::where(['id'=>$item['certid']])->update(['goes'=>2]);
            }else{
                CertCompanyModel::where(['id'=>$item['certid']])->update(['goes'=>2]);
            }
        }
    }
    public function set_cert_goes_shi($data, $bill){
        $list = CertBorrowListModel::where(['mid'=>$data['id']])->select()->toArray();
        foreach ($list as $item){
            if(!$item['guidt'] || $item['guidt'] == '0000-00-00'){
                if($item['type'] == 1){
                    CertModel::where(['id'=>$item['certid']])->update(['goes'=>1]);
                }else{
                    CertCompanyModel::where(['id'=>$item['certid']])->update(['goes'=>1]);
                }
            }
        }
    }






    //更新人员证书数量
    public function vate_person_certnum($arr=[]){


    }

    //更新人员状态
    public function vate_person_status($arr=[]){


    }

    //更新证书状态
    public function vate_cert_status($arr=[]){


    }

    //更新证书去向
    public function vate_cert_goes($arr=[]){


    }

    //更新企业证书状态
    public function vate_certcom_status($arr=[]){


    }

    //更新企业证书去向
    public function vate_certcom_goes($arr=[]){


    }




}
